package com.fh.modules.oms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fh.common.exception.Assert;
import com.fh.common.web.impl.AbstractServiceImpl;
import com.fh.modules.oms.entity.OmsOrderWechat;
import com.fh.modules.oms.entity.OmsOrderWechatItem;
import com.fh.modules.oms.mapper.OmsOrderWechatItemMapper;
import com.fh.modules.oms.service.IOmsOrderWechatItemService;
import com.fh.modules.pms.entity.PmsProduct;
import com.fh.modules.pms.service.IPmsProductService;
import com.fhMall.request.SubmitOrderRequest;
import com.fhMall.request.vo.WechatOrderItemVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Description: 微信订单商品表
 * @author： code generator
 * @date：   2019-10-07
 * @version： V1.0
 */
@Service
public class OmsOrderWechatItemServiceImpl extends AbstractServiceImpl<OmsOrderWechatItemMapper, OmsOrderWechatItem> implements IOmsOrderWechatItemService {

    @Autowired
    private IPmsProductService pmsProductService;
    @Override
    public List<OmsOrderWechatItem> getOrderItem(Integer orderId){
        LambdaQueryWrapper<OmsOrderWechatItem> query=new LambdaQueryWrapper<OmsOrderWechatItem>();
        query.eq(OmsOrderWechatItem::getOrderId,orderId);
        query.eq(OmsOrderWechatItem::getDelFlag,0);
        return this.list(query);
    }
    @Override
    public void addOrderItems(OmsOrderWechat omsOrderWechat, SubmitOrderRequest submitOrderRequest) {
        List<WechatOrderItemVo> wechatOrderItemVos = submitOrderRequest.getOrderItem();
        BigDecimal productTotalPrice=new BigDecimal("0");
        for(WechatOrderItemVo wechatOrderItemVo:wechatOrderItemVos){
            PmsProduct pmsProduct=pmsProductService.getOrderProduct(wechatOrderItemVo.getProductId());
            //验证订单行信息是否正确
            validOrderItem(wechatOrderItemVo,pmsProduct);
            OmsOrderWechatItem omsOrderWechatItem=new OmsOrderWechatItem();
            omsOrderWechatItem.setOrderId(omsOrderWechat.getId());
            BeanUtils.copyProperties(wechatOrderItemVo,omsOrderWechatItem);
            omsOrderWechatItem.setProductName(pmsProduct.getName());
            BigDecimal oneProductPrice=pmsProduct.getPrice().multiply(new BigDecimal(wechatOrderItemVo.getProductQuantity()));
            omsOrderWechatItem.setProductPrice(oneProductPrice);
            productTotalPrice=productTotalPrice.add(oneProductPrice);
            this.save(omsOrderWechatItem);
        }
        //验证订单总价
        validOrderPrice(omsOrderWechat,productTotalPrice);
    }

    /**
     * 验证订单行信息是否正确
     * @param wechatOrderItemVo
     * @param pmsProduct
     */
    private void validOrderItem(WechatOrderItemVo wechatOrderItemVo,PmsProduct pmsProduct){
        Assert.isFalse(pmsProduct.getPrice().compareTo(wechatOrderItemVo.getProductPrice())==0,pmsProduct.getName()+"价格有异，下单失败！");
    }

    /**
     * 验证订单总价
     * @param omsOrderWechat
     * @param productTotalPrice
     */
    private void validOrderPrice(OmsOrderWechat omsOrderWechat, BigDecimal productTotalPrice){
        //优惠总价
        BigDecimal reducePrice=omsOrderWechat.getCouponPrice().add(omsOrderWechat.getDiscountPrice()).add(omsOrderWechat.getIntegrationPrice())
                .add(omsOrderWechat.getPromotionPrice());
        BigDecimal orderProductTotalPrice=omsOrderWechat.getTotalPrice().subtract(omsOrderWechat.getFreightPrice());
        Assert.isTrue(reducePrice.compareTo(productTotalPrice)==1,"优惠总价大于商品总价");
        Assert.isFalse(orderProductTotalPrice.compareTo(productTotalPrice)==0,"订单商品总价有异，下单失败！");
        Assert.isFalse(omsOrderWechat.getTotalPrice().subtract(reducePrice).compareTo(omsOrderWechat.getPayPrice())==0,"订单实际支付价格有异，下单失败！");
    }




}
